<?php
/**
 * Created by zysc.
 * V 28002
 * 呕心力作
 */
namespace z;
class Net
{
    static $isHeader=false;
    static function send($url, $params=null, $method = 'GET', $header = array(), $multi = false){

        $opts = array(
            CURLOPT_CONNECTTIMEOUT=>10,
            CURLOPT_TIMEOUT    => 30,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_HTTPHEADER   => $header
        );
        switch(strtoupper($method)){
            case 'GET':
                $opts[CURLOPT_URL] = $url . ($params?('?' . http_build_query($params)):'');
                break;
            case 'POST':
                $params = $params!==null&&$multi ? $params : http_build_query($params);
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
            default:
                throw new \Exception('不支持的请求方式！');
        }
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        if(self::$isHeader)curl_setopt($ch, CURLOPT_HEADER, true);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        self::clear();
        return $data;
    }
    static function clear(){
        self::$isHeader=false;
    }
}
